package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.QuestionBank;
import com.battle.pojo.entity.QuestionBankDetails;
import com.battle.pojo.param.questionBankDetails.*;
import com.battle.pojo.vo.questionBankDetails.*;
import com.battle.pojo.dto.questionBankDetails.*;
import com.battle.service.repository.in.QuestionBankDetailsRepository;
import com.battle.service.repository.in.QuestionBankRepository;
import com.battle.service.service.in.QuestionBankDetailsService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：题目管理服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class QuestionBankDetailsServiceImpl implements QuestionBankDetailsService {

    private final QuestionBankDetailsRepository questionBankDetailsRepository;
    private final QuestionBankRepository questionBankRepository;

    public QuestionBankDetailsServiceImpl(QuestionBankDetailsRepository questionBankDetailsRepository, QuestionBankRepository questionBankRepository) {
        this.questionBankDetailsRepository = questionBankDetailsRepository;
        this.questionBankRepository = questionBankRepository;
    }


    /**
     * 创建题目管理
     *
     * @param param
     */
    @Override
    public void createQuestionBankDetails(CreateQuestionBankDetailsParam param) {
        QuestionBankDetails questionBankDetails = new QuestionBankDetails();
        BeanUtils.copyProperties(param, questionBankDetails);
        Long id = IdGenerator.SNOW.generate();
        questionBankDetails.setId(id);
        //map转jsonString
        Gson gson = new Gson();
        String options = gson.toJson(param.getOptions());
        questionBankDetails.setOptions(options);

        String answer = gson.toJson(param.getAnswer());
        questionBankDetails.setAnswer(answer);
        questionBankDetailsRepository.create(questionBankDetails);
    }


    /**
     * 批量创建题目管理
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bathCreateQuestionBankDetails(BathCreateQuestionBankDetailsParam param) {
        //map转jsonString
        Gson gson = new Gson();
        if (CollectionUtil.isNotEmpty(param.getDetailsList())){
            List<QuestionBankDetails> questionBankDetailsList = new ArrayList<>();
            for (CreateQuestionBankDetailsParam createQuestionBankDetailsParam : param.getDetailsList()) {
                QuestionBankDetails questionBankDetails = new QuestionBankDetails();
                BeanUtils.copyProperties(createQuestionBankDetailsParam, questionBankDetails);
                Long id = IdGenerator.SNOW.generate();
                questionBankDetails.setId(id);
                questionBankDetails.setQuestionBankId(param.getQuestionBankId());
                //处理复杂json
                String options = gson.toJson(createQuestionBankDetailsParam.getOptions());
                questionBankDetails.setOptions(options);
                String answer = gson.toJson(createQuestionBankDetailsParam.getAnswer());
                questionBankDetails.setAnswer(answer);
                questionBankDetailsList.add(questionBankDetails);
            }
            questionBankDetailsRepository.saveBatch(questionBankDetailsList);
        }
    }

    /**
     * 修改题目管理
     *
     * @param param
     */
    @Override
    public void editQuestionBankDetails(EditQuestionBankDetailsParam param) {
        Long id = param.getId();
        QuestionBankDetails questionBankDetails = questionBankDetailsRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, questionBankDetails);
        //map转jsonString
        Gson gson = new Gson();
        String options = gson.toJson(param.getOptions());
        questionBankDetails.setOptions(options);

        String answer = gson.toJson(param.getAnswer());
        questionBankDetails.setAnswer(answer);
        questionBankDetailsRepository.edit(questionBankDetails);
    }

    /**
     * 移除题目管理
     *
     * @param id
     */
    @Override
    public void removeQuestionBankDetails(Long id) {
        questionBankDetailsRepository.removeById(id);
    }

    /**
     * 启动/禁用题目管理
     *
     * @param id
     */
    @Override
    public void enabledQuestionBankDetails(Long id) {
        QuestionBankDetails questionBankDetails = questionBankDetailsRepository.getByIdCheck(id);
        questionBankDetails.setIsDisable(!questionBankDetails.getIsDisable());
        questionBankDetailsRepository.edit(questionBankDetails);
    }

    /**
     * 获取题目管理详情
     *
     * @param id
     * @return
     */
    @Override
    public GetQuestionBankDetailsVo getQuestionBankDetails(Long id) {
        QuestionBankDetails questionBankDetails = questionBankDetailsRepository.getByIdCheck(id);
        GetQuestionBankDetailsVo vo = new GetQuestionBankDetailsVo();
        BeanUtils.copyProperties(questionBankDetails, vo);
        //处理复杂json
        Gson gson = new Gson();
        Map<String,String> option = gson.fromJson(questionBankDetails.getOptions(), Map.class);
        vo.setOptions(option);
        List<String>  answer = gson.fromJson(questionBankDetails.getAnswer(), List.class);
        vo.setAnswer(answer);

        return vo;
    }

    /**
     * 获取题目管理 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetQuestionBankDetailsListVo> getQuestionBankDetailsList(GetQuestionBankDetailsListParam param) {
        Gson gson = new Gson();
        SelectQuestionBankDetailsDto dto = new SelectQuestionBankDetailsDto();
        BeanUtils.copyProperties(param, dto);
        Page<QuestionBankDetails> page = questionBankDetailsRepository.selectQuestionBankDetails(dto);
        List<QuestionBankDetails> questionBankDetailsList = page.getRecords();
        List<GetQuestionBankDetailsListVo> list = new ArrayList<>();
        for (QuestionBankDetails questionBankDetails : questionBankDetailsList) {
            GetQuestionBankDetailsListVo vo = new GetQuestionBankDetailsListVo();
            BeanUtils.copyProperties(questionBankDetails, vo);
            //处理复杂json
            Map<String,String> option = gson.fromJson(questionBankDetails.getOptions(), Map.class);
            vo.setOptions(option);
            List<String>  answer = gson.fromJson(questionBankDetails.getAnswer(), List.class);
            vo.setAnswer(answer);
            vo.setQuestionId(questionBankDetails.getId());
            //获取题库查询
            QuestionBank bank = questionBankRepository.getById(questionBankDetails.getQuestionBankId());
            if (bank != null) {
                vo.setQuestionBankName(bank.getName());
            }
            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取题目管理下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetQuestionBankDetailsDownListVo> getQuestionBankDetailsDownList(GetQuestionBankDetailsDownListParam param) {
        QuestionBankDetails po = new QuestionBankDetails();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<QuestionBankDetails> questionBankDetailsList = questionBankDetailsRepository.getListByEntity(po);

        List<GetQuestionBankDetailsDownListVo> list = new ArrayList<>();
        for (QuestionBankDetails questionBankDetails : questionBankDetailsList) {
            GetQuestionBankDetailsDownListVo vo = new GetQuestionBankDetailsDownListVo();
            BeanUtils.copyProperties(questionBankDetails, vo);
            list.add(vo);
        }
        return list;
    }


}



